package com.sphd.Restaurant.presenter;

import android.widget.Toast;

import com.android.volley.VolleyError;
import com.sphd.Restaurant.infos.Dishes;
import com.sphd.Restaurant.infos.Sorts;
import com.sphd.Restaurant.infos.VipInfo;
import com.sphd.Restaurant.interfFace.VolleyCallBack;
import com.sphd.Restaurant.utils.JsonUtil;
import com.sphd.Restaurant.utils.VolleyErrorHelper;
import com.sphd.Restaurant.views.OderView;


import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by Hu on 2016/10/11.
 */

public class OrderP extends BasePresenter implements VolleyCallBack<JSONObject> {
    OderView orderView;

    public OrderP(OderView orderView) {
        this.orderView = orderView;
    }

    @Override
    public void addData(String url) {
        orderView.showdialog();
        sendJsonObjectUrl(url, this);
    }

    @Override
    public void onResponse(JSONObject object) {
        try {
            List<Sorts> sortses = JsonUtil.getListFromString(object.getString("sorts"), Sorts.class);
            List<Dishes> dishes = JsonUtil.getListFromString(object.getString("dishes"), Dishes.class);
            orderView.sendLists(sortses, dishes);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        orderView.dissmissdialog();
    }


    @Override
    public void onErrorResponse(VolleyError error) {
orderView.dissmissdialog();Toast.makeText(context, VolleyErrorHelper.getMessage(error),Toast.LENGTH_SHORT).show();
    }

    public void checklimit(String s) {
        orderView.showdialog();
        sendJsonObjectUrl(s, new VolleyCallBack<JSONObject>() {
            @Override
            public void onResponse(JSONObject object) {
                try {
                    List<VipInfo> vipInfos = JsonUtil.getListFromString(object.getString("res"), VipInfo.class);
                    orderView.sendVipList(vipInfos);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                orderView.dissmissdialog();
            }

            @Override
            public void onErrorResponse(VolleyError error) {

                Toast.makeText(context, VolleyErrorHelper.getMessage(error),Toast.LENGTH_SHORT).show();
                orderView.dissmissdialog();
            }
        });
    }
}
